/*
 * Copyright (c) 2024 Huawei Technologies Co., Ltd.
 * openUBMC is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *         http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

#include <devmon/driver_abi.h>
#include <mc/log.h>
// #include <iostream>
// #include <memory>

#include "emulex_card.h"
#include "emulex_port.h"
#include "emulex_om.h"

#ifndef DEVICE_DRIVER_NAME
#define DEVICE_DRIVER_NAME "emulex"
#endif

using namespace dev;

// 导出信息实现
extern "C" {

driver_handle_t create_emulex_card(void* service, const char* name) {
    try {
        if (service == nullptr || name == nullptr) {
            return nullptr;
        }
        emulex_card* device = new emulex_card();
        device->set_service(static_cast<mc::engine::service*>(service));
        device->set_object_name(name);
        return (void*)device;
    } catch (const std::exception& e) {
        elog("Error creating PCIeNicCard: ${error}", ("error", e.what()));
        return nullptr;
    }
}

status_t init_emulex_card(driver_handle_t device, void* csr_object, void* connector) {
    if (device == nullptr || csr_object == nullptr || connector == nullptr) {
        elog("Error initializing emulex PCIeNicCard: device is null");
    }
    emulex_card*          device_object  = static_cast<emulex_card*>(device);
    mc::mutable_dict* csr_object_ptr = static_cast<mc::mutable_dict*>(csr_object);
    mc::dict*         connector_ptr  = static_cast<mc::dict*>(connector);
    bool              ret            = device_object->init(*csr_object_ptr, *connector_ptr);
    if (!ret) {
        return STATUS_ERROR;
    }
    return STATUS_OK;
}

status_t start_emulex_card(driver_handle_t device) {
    if (device == nullptr) {
        elog("Error starting emulex PCIeNicCard: device is null");
        return STATUS_ERROR;
    }
    emulex_card* device_object = static_cast<emulex_card*>(device);
    device_object->start();
    return STATUS_OK;
}

status_t stop_emulex_card(driver_handle_t device) {
    if (device == nullptr) {
        elog("Error stopping emulex PCIeNicCard: device is null");
        return STATUS_ERROR;
    }
    emulex_card* device_object = static_cast<emulex_card*>(device);
    device_object->stop();
    return STATUS_OK;
}

driver_handle_t create_emulex_port(void* service, const char* name) {
    try {
        if (service == nullptr || name == nullptr) {
            elog("Error creating emulex NetworkPort: service or name is null");
            return nullptr;
        }
        emulex_port* device = new emulex_port();
        device->set_service(static_cast<mc::engine::service*>(service));
        device->set_object_name(name);
        return (void*)device;
    } catch (const std::exception& e) {
        elog("Error creating emulex NetworkPort: ${error}", ("error", e.what()));
        return nullptr;
    }
}

status_t init_emulex_port(driver_handle_t device, void* csr_object, void* connector) {
    if (device == nullptr || csr_object == nullptr || connector == nullptr) {
        elog("Error initializing emulex NetworkPort: device is null");
    }
    emulex_port*          device_object  = static_cast<emulex_port*>(device);
    mc::mutable_dict* csr_object_ptr = static_cast<mc::mutable_dict*>(csr_object);
    mc::dict*         connector_ptr  = static_cast<mc::dict*>(connector);
    bool              ret            = device_object->init(*csr_object_ptr, *connector_ptr);
    if (!ret) {
        return STATUS_ERROR;
    }
    return STATUS_OK;
}

status_t start_emulex_port(driver_handle_t device) {
    if (device == nullptr) {
        elog("Error starting emulex NetworkPort: device is null");
        return STATUS_ERROR;
    }
    emulex_port* device_object = static_cast<emulex_port*>(device);
    device_object->start();
    return STATUS_OK;
}

status_t stop_emulex_port(driver_handle_t device) {
    if (device == nullptr) {
        elog("Error stopping emulex NetworkPort: device is null");
        return STATUS_ERROR;
    }
    emulex_port* device_object = static_cast<emulex_port*>(device);
    device_object->stop();
    return STATUS_OK;
}

driver_handle_t create_emulex_optical_module(void* service, const char* name) {
    try {
        if (service == nullptr || name == nullptr) {
            elog("Error creating emulex OpticalModule: service or name is null");
            return nullptr;
        }

        emulex_optical_module* device = new emulex_optical_module();
        device->set_object_name(name);
        device->set_service(static_cast<mc::engine::service*>(service));
        return (void*)device;
    } catch (const std::exception& e) {
        elog("Error creating emulex OpticalModule: ${error}", ("error", e.what()));
        return nullptr;
    }
}

status_t init_emulex_optical_module(driver_handle_t device, void* csr_object, void* connector) {
    if (device == nullptr || csr_object == nullptr || connector == nullptr) {
        elog("Error initializing emulex OpticalModule: device is null");
        return STATUS_ERROR;
    }
    emulex_optical_module* device_object  = static_cast<emulex_optical_module*>(device);
    mc::mutable_dict*      csr_object_ptr = static_cast<mc::mutable_dict*>(csr_object);
    mc::dict*              connector_ptr  = static_cast<mc::dict*>(connector);
    bool                   ret            = device_object->init(*csr_object_ptr, *connector_ptr);
    if (!ret) {
        return STATUS_ERROR;
    }

    return STATUS_OK;
}

status_t start_emulex_optical_module(driver_handle_t device) {
    if (device == nullptr) {
        elog("Error starting emulex OpticalModule: device is null");
        return STATUS_ERROR;
    }
    emulex_optical_module* device_object = static_cast<emulex_optical_module*>(device);
    device_object->start();
    return STATUS_OK;
}

status_t stop_emulex_optical_module(driver_handle_t device) {
    if (device == nullptr) {
        elog("Error stopping emulex OpticalModule: device is null");
        return STATUS_ERROR;
    }
    emulex_optical_module* device_object = static_cast<emulex_optical_module*>(device);
    device_object->stop();
    return STATUS_OK;
}

// pcie_nic_card设备类信息
device_driver_t emulex_card_device_driver = {.device_name = "PCIeNicCard",
                                         .ctor        = create_emulex_card,
                                         .init        = init_emulex_card,
                                         .start       = start_emulex_card,
                                         .stop        = stop_emulex_card};

// network_port设备类信息
device_driver_t emulex_port_device_driver = {.device_name = "NicPort",
                                         .ctor        = create_emulex_port,
                                         .init        = init_emulex_port,
                                         .start       = start_emulex_port,
                                         .stop        = stop_emulex_port};
// optical_module设备类信息
device_driver_t emulex_optical_module_device_driver = {.device_name = "OpticalTransceiver",
                                                       .ctor        = create_emulex_optical_module,
                                                       .init        = init_emulex_optical_module,
                                                       .start       = start_emulex_optical_module,
                                                       .stop        = stop_emulex_optical_module};
// 导出设备类信息
device_driver_t emulex_device_driver[] = {emulex_card_device_driver, emulex_port_device_driver,emulex_optical_module_device_driver};
status_t        register_device_driver(device_driver_t** device_driver, uint8_t* count) {
    *device_driver = emulex_device_driver;
    *count         = sizeof(emulex_device_driver) / sizeof(emulex_device_driver[0]);
    return STATUS_OK;
}
}